#include <iostream>
#include <functional>

using std::cout;
using std::endl;
using std::bind;

int add(int x, int y, int z)
{
    cout << "int add(int, int, int)" << endl;
    return x + y + z;
}

class Example
{
public:
    int add(int x, int y)
    {
        cout << "int Example::add(int, int)" << endl;
        return x + y;
    }

    static void func(int x, int y)
    {
        cout << "x = " << x << ", y = " << y << endl;
    }
};

void test()
{
    //C语言中，函数名就是函数的入口地址
    auto f = bind(&add, 1, 3, 5);
    cout << "f() = " << f() << endl;

    cout << endl;
    Example ex;
    auto f2 = bind(&Example::add, &ex, 10, 20);
    cout << "f2() = " << f2() << endl;

    auto f3 = bind(&Example::func, 30, 70);
    cout << "f3() = ";
    f3();

    /* int a = 100; */
    /* int b = 200; */
    /* auto f4 = bind(&add, 10, a, b); */
    using namespace std::placeholders;
    auto f4 = bind(&add, 10, _1, _3);
    cout << "f4(100, 200) = " << f4(100, 200, 300, 400, 500) << endl;
}

int func1()
{
    cout << "int func1()" << endl;
    return 10;
}

int func2()
{
    cout << "int func2()" << endl;
    return 20;
}

void test2()
{
    int (*pFunc)();

    pFunc = func1;
    cout << "pFunc() = " << pFunc() << endl;

    pFunc = &func2;
    cout << "pFunc() = " << pFunc() << endl;

}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

